﻿Imports System.Threading
Imports System.Timers
Imports HslCommunication.Profinet.Siemens
Imports System.ComponentModel
Imports HslCommunication
Imports HslCommunication.Controls
Imports HslCommunication.LogNet
Public Class frmRGVcontrol

    Private active_DB As String = "DB16."

    Private car1Ip As String

    Private car2Ip As String

    Private g_szInitFileName As String

    Private _1F_JBGW_PLC As SiemensS7Net
    Private _1G2_JBGW_PLC As SiemensS7Net
    Private _1G3_JBGW_PLC As SiemensS7Net
    Private _2_JBGW_PLC As SiemensS7Net

    Private _1F_Connected As Boolean
    Private _1G2_Connected As Boolean
    Private _1G3_Connected As Boolean
    Private _2_Connected As Boolean

    Private _JBGW_PLC As SiemensS7Net
    Private _RGV_PLC As SiemensS7Net

    Private _Current_Car_Name As String = ""
    Private _Heard_Num As Integer = 0

    Private _connectSuccess As Boolean
    Private _heard As Boolean = False
    Private th As Thread
    '1车间
    'Private WithEvents tmFLBUPWait As New System.Timers.Timer(1000)
    'Private WithEvents tmGJBUPWait As New System.Timers.Timer(1000)
    'Private WithEvents tmGJBUPWait2 As New System.Timers.Timer(1000)
    '2车间
    'Private WithEvents tm2FLBUPWait As New System.Timers.Timer(1000)
    'Private WithEvents tm2GJBUPWait As New System.Timers.Timer(1000)

    Private thUPWait As Thread '上料线程
    Private thDNWait As Thread 'New Thread(AddressOf thDNWait_Elapsed) '下料线程

    'Private WithEvents timer1 As New System.Timers.Timer 'RGV车
    Private WithEvents timer2 As New System.Timers.Timer(500) '接驳工位
    Private RGV_READ_TH As Thread
    Private RGV_READ_FLG As Boolean = False
    Private _Current_MDD As Integer = 0
    Private LocPoing As Point
    Private logfile As ILogNet
    Private logfileHeard As ILogNet
    Private _monitor As Boolean = True
    Private DN_CMD As String

    Private Sub frmRGVcontrol_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        If Not HslCommunication.Authorization.SetAuthorizationCode("e3e27344-1a9f-4d8b-b4fc-da5cae4b99bf") Then

        End If
        If Not HslControls.Authorization.SetAuthorizationCode("ce5ade06-6f14-4b0d-b9c2-0d625664dc3f") Then

        End If
        Dim d As String = My.Application.Info.Version.Major & "." & My.Application.Info.Version.Minor & "." & My.Application.Info.Version.Build
        Me.Text = Me.Text & "   (Ver." & d & ")"
        Me.g_szInitFileName = My.Application.Info.DirectoryPath & "\RGV.ini"
        logfile = New LogNetDateTime(Application.StartupPath & "\\Logs", GenerateMode.ByEveryDay)
        logfileHeard = New LogNetDateTime(Application.StartupPath & "\\HeardLogs", GenerateMode.ByEveryDay)

        btnAuto.BackColor = Color.White
        '连接数据库
        connectDB()
        Control.CheckForIllegalCrossThreadCalls = False
        'Me.th = New Thread(AddressOf active)
        'Me.th.Start()
        connectJBGW()
        timer2.Start()
        cmbLK.SelectedIndex = 0

    End Sub
    Public Property ConnectSuccess As Boolean
        Get
            Return _connectSuccess
        End Get
        Set(value As Boolean)
            _connectSuccess = value
            If value Then

                UcRGV1.lanter.LanternBackground = Color.Green
                UcRGV1.Battery.Visible = True
                'timer1.Start()

            Else

                UcRGV1.lanter.LanternBackground = Color.Silver
                UcRGV1.Battery.Visible = False
                'timer1.Stop()
            End If
        End Set
    End Property

    Public Sub New()
        ''加载DLL到exe的事件
        AddHandler AppDomain.CurrentDomain.AssemblyResolve, New ResolveEventHandler(AddressOf CurrentDomain_AssemblyResolve)
        InitializeComponent()
    End Sub

    Private Sub ComboBox1_DrawItem(sender As Object, e As DrawItemEventArgs) Handles cmbLK.DrawItem
        If e.Index < 0 Then
            Exit Sub
        End If
        e.DrawBackground()
        e.DrawFocusRectangle()
        e.Graphics.DrawString(cmbLK.Items(e.Index).ToString(), e.Font, New SolidBrush(e.ForeColor), e.Bounds.X, e.Bounds.Y + 10)
    End Sub

    Private Function CurrentDomain_AssemblyResolve(sender As Object, args As ResolveEventArgs) As System.Reflection.Assembly
        Dim dllName As String = If(args.Name.Contains(","), args.Name.Substring(0, args.Name.IndexOf(","c)), args.Name.Replace(".dll", ""))
        dllName = dllName.Replace(".", "_")
        If dllName.EndsWith("_resources") Then
            Return Nothing
        End If
        Dim rm As New System.Resources.ResourceManager([GetType].Namespace & ".Resources", System.Reflection.Assembly.GetExecutingAssembly())
        Dim bytes As Byte() = DirectCast(rm.GetObject(dllName), Byte())
        Return System.Reflection.Assembly.Load(bytes)
    End Function
    Delegate Sub SetDDlabelDet()
    Dim setDDLabelCaller As SetDDlabelDet
    Private Sub setDDLabel(_flg As Integer, _text As String, _visible As Boolean, _point As Point)
        If _flg = 1 Then
            lblDD.Visible = _visible
        End If
        If _flg = 2 Then
            lblDD.Text = _text
        End If
        If _flg = 3 Then
            lblDD.Visible = _visible
            lblDD.Location = _point
        End If
    End Sub
    Private Sub btnActive_Click(sender As Object, e As EventArgs) Handles btnActive.Click
        If Not rdoCar1.Checked And Not rdoCar2.Checked Then
            seterrInfo("请选择RGV车辆！")
            Exit Sub
        End If

        If lblMdd.Text = "" Then
            seterrInfo("请选择目的地！")
            Exit Sub
        End If
        If _connectSuccess = False Then
            lblconnect.Text = "连接失败"
            lblconnect.BackColor = Color.Red
            lblconnect.ForeColor = Color.White
            seterrInfo("RGV 连接失败，无法启动。")
            Exit Sub
        End If
        '光栅判断-----------

        Dim arriveResult As OperateResult(Of Integer) = _RGV_PLC.ReadInt32("DB17.4.0")
        If arriveResult.IsSuccess = True Then
            Select Case arriveResult.Content
                Case 101, 102, 103 '1车间法兰板
                    If _1F_Connected AndAlso _1F_JBGW_PLC.ReadBool("DB1.DBX20.1").IsSuccess Then
                        If _1F_JBGW_PLC.ReadBool("DB1.DBX20.1").Content = False Then
                            seterrInfo("光栅被档，不允许移动。")
                            Exit Sub
                        End If
                    End If
                Case 3, 103 '1车间A线
                    If _1G2_Connected AndAlso _1G2_JBGW_PLC.ReadBool("DB15.DBX20.5").IsSuccess Then
                        If _1G2_JBGW_PLC.ReadBool("DB15.DBX20.5").Content = False Then
                            seterrInfo("光栅被档，不允许移动。")
                            Exit Sub
                        End If
                    End If
                Case 4, 104 '1车间空线
                    If _1G2_Connected AndAlso _1G2_JBGW_PLC.ReadBool("DB15.DBX20.7").IsSuccess Then
                        If _1G2_JBGW_PLC.ReadBool("DB15.DBX20.7").Content = False Then
                            seterrInfo("光栅被档，不允许移动。")
                            Exit Sub
                        End If
                    End If
                Case 5, 105 '1车间B线
                    If _1G2_Connected AndAlso _1G2_JBGW_PLC.ReadBool("DB15.DBX20.6").IsSuccess Then
                        If _1G2_JBGW_PLC.ReadBool("DB15.DBX20.6").Content = False Then
                            seterrInfo("光栅被档，不允许移动。")
                            Exit Sub
                        End If
                    End If
                Case 9, 109, 110 '1车间骨架板
                    If _1G3_Connected AndAlso _1G3_JBGW_PLC.ReadBool("DB1.DBX20.1").IsSuccess Then
                        If _1G3_JBGW_PLC.ReadBool("DB1.DBX20.1").Content = False Then
                            seterrInfo("光栅被档，不允许移动。")
                            Exit Sub
                        End If
                    End If
                Case 11， 12, 111, 112 '2车间法兰板
                    If _2_Connected AndAlso _2_JBGW_PLC.ReadBool("DB200.DBX121.3").IsSuccess Then
                        If _2_JBGW_PLC.ReadBool("DB200.DBX121.3").Content = False Then
                            seterrInfo("光栅被档，不允许移动。")
                            Exit Sub
                        End If
                    End If
                Case 13， 14, 113, 114 '2车间骨架板
                    If _2_Connected AndAlso _2_JBGW_PLC.ReadBool("DB200.DBX121.4").IsSuccess Then
                        If _2_JBGW_PLC.ReadBool("DB200.DBX121.4").Content = False Then
                            seterrInfo("光栅被档，不允许移动。")
                            Exit Sub
                        End If
                    End If
            End Select
        End If
        '光栅判断-----------


        lblDD.Visible = False
        'Me.Invoke(setDDLabelCaller, 1, "", False, Nothing)
        _Current_MDD = 0

        Dim keyName As String = (UcRGV1.StationNo + 1).ToString + "#-" + IIf(lblMdd.Text = "充电位", "CDW", lblMdd.Text)
        Dim iniFile As String = modWinApi.GetIniFile("address", keyName, "", Me.g_szInitFileName)

        If iniFile = "" Then
            seterrInfo("目的地取得错误！")
            Exit Sub
        End If

        If send(iniFile, True) Then
            'pnlInfo.Visible = True
            'pnlInfo.Refresh()

            _Current_MDD = CInt(GetIniFile("Station", keyName, "", Me.g_szInitFileName))

            lblDD.Text = "<- RGV移动中..."
            'Me.Invoke(setDDLabelCaller, 2, "<- RGV移动中...", False, Nothing)

            For Each rdo As Control In gbAuto.Controls
                If rdo.GetType.ToString = "System.Windows.Forms.RadioButton" Then
                    If CType(rdo, RadioButton).Checked = True Then
                        LocPoing = New Point(rdo.Location.X + rdo.Width + 5, rdo.Location.Y)
                    End If
                End If
            Next
            'Me.Invoke(setDDLabelCaller, 3, "", True, LocPoing)
            lblDD.Location = LocPoing
            lblDD.Visible = True

        End If

    End Sub

    Private Sub rdoCar1_CheckedChanged(sender As Object, e As EventArgs) Handles rdoCar2.CheckedChanged, rdoCar1.CheckedChanged
        UcRGV1.CarName = sender.text
        lblconnect.Visible = False
    End Sub

    Private Sub btnConnect_Click(sender As Object, e As EventArgs) Handles btnConnect.Click
        Me.car1Ip = GetIniFile("IP", "CAR1", "127.0.0.1", Me.g_szInitFileName)
        Me.car2Ip = GetIniFile("IP", "CAR2", "127.0.0.1", Me.g_szInitFileName)

        If btnConnect.Text() = "连接RGV" Then
            Dim iP As String = "127.0.0.1"

            If rdoCar1.Checked Then
                iP = car1Ip
                _Current_Car_Name = "1"
            End If

            If rdoCar2.Checked Then
                iP = car2Ip
                _Current_Car_Name = "2"
            End If

            If iP = "127.0.0.1" Then
                seterrInfo("请选择RGV车。")
                Exit Sub
            End If

            lblconnect.Visible = True
            lblconnect.Text = "尝试连接中..."
            lblconnect.BackColor = Color.Yellow
            lblconnect.ForeColor = Color.Black
            'Me.Refresh()
            Me.connect(iP)
            If btnAuto.BackColor = Color.White Then
                UcRGV1.手动自动 = 0
            Else
                UcRGV1.手动自动 = 1
            End If

            If _connectSuccess Then
                _Heard_Num = 0
                lblconnect.Visible = True
                lblconnect.Text = "连接成功"
                lblconnect.BackColor = Color.Green
                lblconnect.ForeColor = Color.White
                'btnStop.Selected = UcRGV1.StopCmd
                rdoCar1.Enabled = False
                rdoCar2.Enabled = False

                'If btnStop.Selected() Then
                '    btnStop.Text = "急  停  中"
                'Else
                '    btnStop.Text = "急  停"
                'End If
                RemoveHandler btnStop.Click, AddressOf btnStop_Click

                If _RGV_PLC.ReadBool("DB16.4.4").Content Or _RGV_PLC.ReadBool("DB17.27.2").Content Then
                    btnStop.Text = "急  停  中"
                    UcRGV1.lanStop.Visible = True
                    btnStop.Selected = True
                Else
                    btnStop.Text = "急  停"
                    UcRGV1.lanStop.Visible = False
                    btnStop.Selected = False
                End If
                AddHandler btnStop.Click, AddressOf btnStop_Click

                swhDoor.SwitchStatus = UcRGV1.DoorOpen
                btnConnect.Text = "关闭连接"
                RGV_READ_TH = New Thread(AddressOf readPLC)
                RGV_READ_FLG = True
                RGV_READ_TH.Start()
                'timer1.Interval = 500
                'timer1.Start()
            Else
                _Heard_Num = 0
                _Current_Car_Name = ""
                lblconnect.Text = "连接失败"
                lblconnect.BackColor = Color.Red
                lblconnect.ForeColor = Color.White
            End If
        Else
            rdoCar1.Enabled = True
            rdoCar2.Enabled = True
            RGV_READ_FLG = False
            Try
                If RGV_READ_TH.IsAlive Then
                    RGV_READ_TH.Abort()
                End If
            Catch ex As Exception

            End Try
            'timer1.Stop()
            Me.disconnect()
            btnConnect.Text = "连接RGV"
            lblconnect.Text = ""
            btnStop.Selected = False
            btnStop.Text = "急  停"
            swhDoor.SwitchStatus = True

        End If
    End Sub
    Private Current_MDD As String
    Private Current_StationNo As String
    ''' <summary>
    ''' 上下料
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub up_dn_Click(sender As Object, e As EventArgs) Handles up1.Click, dn1.Click
        Current_MDD = lblMdd.Text
        Current_StationNo = UcRGV1.StationNo
        If Not rdoCar1.Checked And Not rdoCar2.Checked Then
            seterrInfo("请选择RGV车辆！")
            Exit Sub
        End If

        If lblMdd.Text = "" Then
            seterrInfo("请选择目的地！")
            Exit Sub
        End If

        'If _connectSuccess = False Then
        If _connectSuccess = False OrElse _RGV_PLC.ReadBool("DB17.26.0").IsSuccess = False Then
            If IsNothing(_RGV_PLC) = False Then _RGV_PLC.ConnectClose()
            lblconnect.Text = "失去连接,请点击[连接RGV]再次尝试。"
            lblconnect.BackColor = Color.Red
            lblconnect.ForeColor = Color.White
            btnConnect.Text = "连接RGV"

            seterrInfo("RGV 连接失败，无法启动。")
            Exit Sub
        ElseIf chkZS.Checked And jbgwConn() = False Then '确定使用那个PLC连接
            seterrInfo("接驳工位 连接失败，无法启动。")
            Exit Sub
        End If

        'readPLC()
        If UcRGV1.lblCarMove.Text <> "静止" Then
            seterrInfo("车辆移动中，不能进行上下料")
            Exit Sub
        End If


        Dim locName As String = (Current_StationNo + 1).ToString + "#-" + Current_MDD
        If locName.Substring(6) = "S" Then
            If Not sender.name.ToUpper = "UP1" Then
                seterrInfo("动作错误")
                Exit Sub
            End If
        End If
        If locName.Substring(6) = "X" Then
            If Not sender.name.ToUpper = "DN1" Then
                seterrInfo("动作错误")
                Exit Sub
            End If
        End If

        '================是否到达目的地==================

        Dim locNo As Integer = CInt(GetIniFile("Station", locName, "", Me.g_szInitFileName))
        If locNo <> _RGV_PLC.ReadInt32("DB17.4.0").Content Then
            seterrInfo("RGV 未移动到位，不能进行 上下料。")
            Exit Sub
        End If
        '================================================
        ' The following expression was wrapped in a checked-expression
        Dim keyName As String = CInt(Current_StationNo + 1).ToString & "#" & IIf(lblFGFlg.Text = "0", "FLB", "GJB") & sender.tag
        Dim iniFile As String = GetIniFile("active", keyName, "", Me.g_szInitFileName)

        _RGV_PLC.Write("DB17.27.4", False) '1#下料完成	Bool	DB17.27.4
        _RGV_PLC.Write("DB17.27.3", False) '2#下料完成	Bool	DB17.27.3
        _RGV_PLC.Write("DB17.27.6", False) '1#上料完成	Bool	DB17.27.6
        _RGV_PLC.Write("DB17.27.7", False) '2#上料完成	Bool	DB17.27.7

        If chkZS.Checked Then
            If Current_MDD = "1FKX" Or Current_MDD = "1FMS" Then
                yiCheJianFlbGjb3(sender, iniFile)
            End If
            If Current_MDD = "1GMS-3" Or Current_MDD = "1GKX-3" Then
                yiCheJianFlbGjb3(sender, iniFile)
            End If
            If Current_MDD = "1GMS1-1" Or Current_MDD = "1GMS1-2" Or Current_MDD = "1GKX-1" Then
                yiCheJianGJB2(sender, iniFile)
            End If

            If Current_MDD = "2FMX" Or Current_MDD = "2FKS" Then
                FLB_2CJ(sender, iniFile)
            End If
            If Current_MDD = "2GMX" Or Current_MDD = "2GKS" Then
                GJB_2CJ(sender, iniFile)
            End If
        End If

    End Sub

    Private Function letJBGWdo() As Boolean
        'Return True

        Dim nowDtEnd As DateTime = DateAdd(DateInterval.Second, 7, Now)
        Dim letJBGWdoFlg As Boolean = False

        'pnlWait.BringToFront()
        'pnlWait.Visible = True
        'lblWait.Text = String.Format("等待RGV动作 ({0})", 0)
        seterrInfo(String.Format("等待RGV动作 ({0})", 0))
        Dim t As Integer = 0
        While Now < nowDtEnd

            If getCarRollerStopStatus(Convert.ToString(_RGV_PLC.ReadInt32("DB17.8.0").Content, 2), Current_StationNo) = False Then
                letJBGWdoFlg = True
                Exit While
            End If
            'lblWait.Text = String.Format("等待RGV动作 ({0})", t)
            'lblWait.Refresh()
            'btnWaitClose.Refresh()
            seterrInfo(String.Format("等待RGV动作 ({0})", t))
            Thread.Sleep(1000)
            t += 1
        End While
        'pnlWait.BringToFront()
        'pnlWait.Visible = False
        'lblWait.Text = ""
        btnWaitClose.PerformClick()
        '------------------------------------
        'If UcRGV1.StationNo = 0 Then
        '    While Now < nowDtAdd5s
        '        readPLC()
        '        If UcRGV1.Stop_0 = False Then
        '            letJBGWdoFlg = True
        '            Exit While
        '        End If
        '        Thread.Sleep(200)
        '    End While
        'Else
        '    While Now < nowDtAdd5s
        '        readPLC()
        '        If UcRGV1.Stop_0 = False Then
        '            letJBGWdoFlg = True
        '            Exit While
        '        End If
        '        Thread.Sleep(200)
        '    End While
        'End If
        Return letJBGWdoFlg
    End Function

    Public Function getCarRollerStopStatus(recStr As String, rollerFlg As Integer) As Boolean
        Dim r1 As Boolean = False
        Dim r2 As Boolean = False
        If recStr <> "" Then
            recStr = recStr.PadLeft(32, "0")
            Dim i As Integer = 2
            Dim array As Integer() = New Integer(16 - 1) {}
            Dim num As Integer = 0
            While i < 34
                array(num) = recStr.Substring(32 - i, 2)
                i += 2
                num += 1
            End While

            If array(8) = 10 Then
                r1 = False
            Else
                If array(8) = 1 Then
                    r1 = False
                End If
            End If

            If array(9) = 10 Then
                r2 = False
            Else
                If array(9) = 1 Then
                    r2 = False
                End If
            End If

            If array(10).ToString().PadLeft(2, "0").Substring(1, 1) = "1" Then
                r1 = True '停
            End If

            If array(10).ToString().PadLeft(2, "0").Substring(0, 1) = "1" Then
                r2 = True '停
            End If

        End If
        If rollerFlg = 0 Then
            Return r1
        Else
            Return r2
        End If
    End Function
    Private Sub FLB_2CJ(sender As Object, _carAct As String)
        Dim cmd As String = ""
        Dim allowFlg As String = ""

        If sender.name = "up1" Then
            cmd = "DB200.DBX77.1"
            allowFlg = "DB200.DBX120.6"
        ElseIf sender.name = "dn1" Then
            cmd = "DB200.DBX77.0"
            allowFlg = "DB200.DBX111.1"
            DN_CMD = "DB200.DBX111.2"
        End If

        If _JBGW_PLC.ReadBool(allowFlg).Content = False Then
            seterrInfo("接驳工位 不允许 上下料。")
            Exit Sub
        End If
        If send(_carAct, True) = False Then
            seterrInfo(sender.text & "  动作错误！")
            Exit Sub
        End If
        '判断滚轮是否转动
        If letJBGWdo() = False Then
            seterrInfo("RGV车辆滚轮未转动，不能上下料。")
            Exit Sub
        End If
        If jbgw(cmd, True) Then
            Thread.Sleep(500)
            Try
                If lblFGFlg.Text = "0" Then '法兰板
                    If sender.name = "dn1" Then
                        Me.jbgw(cmd, False)
                        thDNWait = New Thread(AddressOf thDNWait_Elapsed)
                        thDNWait.Start()
                    Else
                        Me.jbgw(cmd, False)
                        'tm2FLBUPWait.Start()
                        thUPWait = New Thread(AddressOf tm2FlbUpWait_Elapsed)
                        thUPWait.Start()
                    End If
                End If
            Catch ex As Exception
                seterrInfo("RGV控制程序出错，请联系管理员。")
            logfile.WriteInfo(String.Format("{0}{1}出错：{2}"， Current_MDD， sender.name, ex.Message))
            End Try
        End If
    End Sub

    Private Sub GJB_2CJ(sender As Object, _carAct As String)
        Dim cmd As String = ""
        Dim allowFlg As String = ""
        If sender.name = "up1" Then
            cmd = "DB200.DBX77.4"
            allowFlg = "DB200.DBX120.7"
        ElseIf sender.name = "dn1" Then
            cmd = "DB200.DBX77.3"
            allowFlg = "DB200.DBX111.4"
            DN_CMD = "DB200.DBX111.5"
        End If

        If _JBGW_PLC.ReadBool(allowFlg).Content = False Then
            seterrInfo("接驳工位 不允许 上下料。")
            Exit Sub
        End If
        If send(_carAct, True) = False Then
            seterrInfo(sender.text & "  动作错误！")
            Exit Sub
        End If
        '判断滚轮是否转动
        If letJBGWdo() = False Then
            seterrInfo("RGV车辆滚轮未转动，不能上下料。")
            Exit Sub
        End If
        If jbgw(cmd, True) Then
            Thread.Sleep(500)
            Try
                If lblFGFlg.Text = "1" Then '骨架板
                    If sender.name = "dn1" Then
                        Me.jbgw(cmd, False)
                        thDNWait = New Thread(AddressOf thDNWait_Elapsed)
                        thDNWait.Start()
                    Else
                        Me.jbgw(cmd, False)

                        'tm2GJBUPWait.Start()
                        thUPWait = New Thread(AddressOf tm2GJBUpWait_Elapsed)
                        thUPWait.Start()
                    End If
                End If
            Catch ex As Exception
                seterrInfo("RGV控制程序出错，请联系管理员。")
                logfile.WriteInfo(String.Format("{0}{1}出错：{2}"， Current_MDD， sender.name, ex.Message))
            End Try
        End If
    End Sub


    Private Sub yiCheJianGJB2(sender As Object, _carAct As String)
        Dim cmd As String = ""
        Dim allowFlg As String = ""
        If Current_MDD = "1GMS1-1" Then 'A线满框上料MES命令
            allowFlg = "DB15.DBX20.2"
            cmd = "DB15.DBX0.0"
        ElseIf Current_MDD = "1GMS1-2" Then 'B线满框上料MES命令
            cmd = "DB15.DBX0.2"
            allowFlg = "DB15.DBX20.3"
        ElseIf Current_MDD = "1GKX-1" Then '空框下料MES命令
            cmd = "DB15.DBX0.4"
            allowFlg = "DB15.DBX0.5"
            DN_CMD = "DB15.DBX1.6"
        End If

        If _JBGW_PLC.ReadBool(allowFlg).Content = False Then
            seterrInfo("接驳工位 不允许 上下料。")
            Exit Sub
        End If
        If send(_carAct, True) = False Then
            seterrInfo(sender.text & "  动作错误！")
            Exit Sub
        End If
        '判断滚轮是否转动
        If letJBGWdo() = False Then
            seterrInfo("RGV车辆滚轮未转动，不能上下料。")
            Exit Sub
        End If
        If jbgw(cmd, True) Then
            Thread.Sleep(500)
            Try
                If lblFGFlg.Text = "1" Then '骨架板
                    If sender.name = "dn1" Then
                        Me.jbgw(cmd, False)
                        thDNWait = New Thread(AddressOf thDNWait_Elapsed)
                        thDNWait.Start()
                    Else
                        Me.jbgw(cmd, False)
                        thUPWait = New Thread(AddressOf tmGJBUpWait2_Elapsed)
                        thUPWait.Start()
                    End If
                End If
            Catch ex As Exception
                seterrInfo("RGV控制程序出错，请联系管理员。")
                logfile.WriteInfo(String.Format("{0}{1}出错：{2}"， Current_MDD， sender.name, ex.Message))
            End Try

        Else
            btnStop.PerformClick()
            seterrInfo("接驳工位启动失败！RGV已急停")
        End If
    End Sub
    Private Sub tmGJBUpWait2_Elapsed() 'sender As Object, e As ElapsedEventArgs) Handles tmGJBUPWait2.Elapsed

        Dim _cmd1 As String = ""
        Dim _cmd2 As String = ""
        While True
            Try
                seterrInfo("等待上料完成" & vbCrLf & vbCrLf & " 完成前，不要关闭屏幕！！！！")
                UcRGV1._1下料完成 = _RGV_PLC.ReadBool("DB17.27.4").Content() '1#下料完成	Bool	DB17.27.4
                UcRGV1._2下料完成 = _RGV_PLC.ReadBool("DB17.27.3").Content() '2#下料完成	Bool	DB17.27.3
                UcRGV1._1上料完成 = _RGV_PLC.ReadBool("DB17.27.6").Content() '1#上料完成	Bool	DB17.27.6
                UcRGV1._2上料完成 = _RGV_PLC.ReadBool("DB17.27.7").Content() '2#上料完成	Bool	DB17.27.7
                UcRGV1.StopCmd = _RGV_PLC.ReadBool("DB16.4.4").Content()

                If Current_MDD = "1GMS1-1" Then 'A线满框上料MES命令
                    _cmd1 = "DB15.DBX0.0"
                    _cmd2 = "DB15.DBX0.1"
                ElseIf Current_MDD = "1GMS1-2" Then 'B线满框上料MES命令
                    _cmd1 = "DB15.DBX0.2"
                    _cmd2 = "DB15.DBX0.3"
                End If
                If Current_StationNo = 0 Then

                    If UcRGV1._1上料完成 Or UcRGV1.StopCmd Then ' UcRGV1.Stop_0 Or UcRGV1.StopCmd Then
                        logfile.WriteInfo("1车间骨架板" & Current_MDD & "上料完成Begin：滚轮" & Current_StationNo + 1)
                        'Me.jbgw(_cmd1, False)
                        'Thread.Sleep(500)
                        If Me.jbgw(_cmd2, True) Then
                            Thread.Sleep(1000)
                            Me.jbgw(_cmd2, False)
                            pnlWait.Visible = False
                            logfile.WriteInfo("1车间骨架板" & Current_MDD & "上料完成end：滚轮" & Current_StationNo + 1)
                            Exit While
                        Else
                            logfile.WriteInfo("再次设定" & _cmd2 & "=true")
                            Me.jbgw(_cmd2, True)
                        End If
                    End If
                Else
                    If UcRGV1._2上料完成 Or UcRGV1.StopCmd Then 'UcRGV1.Stop_1 Or UcRGV1.StopCmd Then
                        logfile.WriteInfo("1车间骨架板" & Current_MDD & "上料完成Begin：滚轮" & Current_StationNo + 1)
                        'Me.jbgw(_cmd1, False)
                        'Thread.Sleep(500)
                        If Me.jbgw(_cmd2, True) Then
                            Thread.Sleep(1000)
                            Me.jbgw(_cmd2, False)
                            'tmGJBUPWait2.Stop()
                            pnlWait.Visible = False
                            logfile.WriteInfo("1车间骨架板" & Current_MDD & "上料完成end：滚轮" & Current_StationNo + 1)
                            Exit While
                        Else
                            logfile.WriteInfo("再次设定" & _cmd2 & "=true")
                            Me.jbgw(_cmd2, True)
                        End If
                    End If
                End If
            Catch ex As Exception
                logfile.WriteError("1车间骨架板" & Current_MDD & "上料错误：" & ex.Message)
                MsgBox("读取RGV数据错误，关闭此信息后，会给接驳工位发信完成信号。" & vbCr & ex.Message)
                Me.jbgw(_cmd2, True)
                Thread.Sleep(500)
                Me.jbgw(_cmd2, False)
                'tmGJBUPWait2.Stop()
                pnlWait.Visible = False
                Exit While
            End Try
            Thread.Sleep(500)
        End While
        'pnlWait.BringToFront()
        'pnlWait.Visible = True
        'lblWait.Text = "等待上料完成"

    End Sub


    Private Sub yiCheJianFlbGjb3(sender As Object， _carAct As String)
        Dim cmd As String = ""
        Dim allowFlg As String = ""
        If sender.name = "up1" Then
            allowFlg = "DB1.DBX1.6"
            cmd = "DB1.DBX0.0"
        ElseIf sender.name = "dn1" Then
            allowFlg = "DB1.DBX0.3"
            cmd = "DB1.DBX0.1"
            DN_CMD = "DB1.DBX1.5"
        End If

        If _JBGW_PLC.ReadBool(allowFlg).Content = False Then
            seterrInfo("接驳工位 不允许 上下料。")
            Exit Sub
        End If

        If send(_carAct, True) = False Then
            seterrInfo(sender.text & "  动作错误！")
            Exit Sub
        End If

        '判断滚轮是否转动
        If letJBGWdo() = False Then
            seterrInfo("RGV车辆滚轮未转动，不能上下料。")
            Exit Sub
        End If

        If jbgw(cmd, True) Then
            Thread.Sleep(500)
            Try

                'If sender.name = "dn1" Then
                '    Me.jbgw(cmd, False)
                'End If
                If lblFGFlg.Text = "0" Then '法兰板
                    If sender.name = "dn1" Then
                        jbgw(cmd, False)
                        thDNWait = New Thread(AddressOf thDNWait_Elapsed)
                        thDNWait.Start()
                    Else
                        jbgw(cmd, False)
                        'tmFLBUPWait.Start()
                        thUPWait = New Thread(AddressOf tmFlbUpWait_Elapsed)
                        thUPWait.Start()
                    End If
                ElseIf lblFGFlg.Text = "1" Then '骨架板
                    If sender.name = "dn1" Then
                        Me.jbgw(cmd, False)
                        thDNWait = New Thread(AddressOf thDNWait_Elapsed)
                        thDNWait.Start()
                    Else
                        Me.jbgw(cmd, False)
                        'tmGJBUPWait.Start()
                        thUPWait = New Thread(AddressOf tmGJBUpWait_Elapsed)
                        thUPWait.Start()
                    End If
                End If
            Catch ex As Exception
                seterrInfo("RGV控制程序出错，请联系管理员。")
                logfile.WriteInfo(String.Format("{0}{1}出错：{2}"， Current_MDD， sender.name, ex.Message))
            End Try
        Else
            btnStop.PerformClick()
            seterrInfo("接驳工位启动失败！RGV已急停")
        End If
    End Sub

    Private Sub tmGJBUpWait_Elapsed() 'sender As Object, e As ElapsedEventArgs) Handles tmGJBUPWait.Elapsed
        'pnlWait.BringToFront()
        'pnlWait.Visible = True
        'lblWait.Text = "等待上料完成"
        While True
            Try
                seterrInfo("等待上料完成" & vbCrLf & vbCrLf & " 完成前，不要关闭屏幕！！！！")
                UcRGV1._1下料完成 = _RGV_PLC.ReadBool("DB17.27.4").Content() '1#下料完成	Bool	DB17.27.4
                UcRGV1._2下料完成 = _RGV_PLC.ReadBool("DB17.27.3").Content() '2#下料完成	Bool	DB17.27.3
                UcRGV1._1上料完成 = _RGV_PLC.ReadBool("DB17.27.6").Content() '1#上料完成	Bool	DB17.27.6
                UcRGV1._2上料完成 = _RGV_PLC.ReadBool("DB17.27.7").Content() '2#上料完成	Bool	DB17.27.7
                UcRGV1.StopCmd = _RGV_PLC.ReadBool("DB16.4.4").Content()
                If Current_StationNo = 0 Then

                    If UcRGV1._1上料完成 Or UcRGV1.StopCmd Then 'UcRGV1.Stop_0 Or UcRGV1.StopCmd Then
                        logfile.WriteInfo("1车间骨架板上料完成Begin：滚轮" & Current_StationNo + 1)
                        'Me.jbgw("DB1.DBX0.0", False)
                        'Thread.Sleep(500)
                        If Me.jbgw("DB1.DBX0.2", True) Then
                            Thread.Sleep(1000)
                            Me.jbgw("DB1.DBX0.2", False)
                            'tmGJBUPWait.Stop()
                            pnlWait.Visible = False
                            logfile.WriteInfo("1车间骨架板上料完成end：滚轮" & Current_StationNo + 1)
                            Exit While
                        Else
                            logfile.WriteInfo("再次设定DB1.DBX0.2=true")
                            Me.jbgw("DB1.DBX0.2", True)
                        End If
                    End If
                Else
                    If UcRGV1._2上料完成 Or UcRGV1.StopCmd Then 'UcRGV1.Stop_1 Or UcRGV1.StopCmd Then
                        logfile.WriteInfo("1车间骨架板上料完成Begin：滚轮" & Current_StationNo + 1)
                        'Me.jbgw("DB1.DBX0.0", False)
                        'Thread.Sleep(500)
                        If Me.jbgw("DB1.DBX0.2", True) Then
                            Thread.Sleep(1000)
                            Me.jbgw("DB1.DBX0.2", False)
                            'tmGJBUPWait.Stop()
                            pnlWait.Visible = False
                            logfile.WriteInfo("1车间骨架板上料完成end：滚轮" & Current_StationNo + 1)
                            Exit While
                        Else
                            logfile.WriteInfo("再次设定DB1.DBX0.2=true")
                            Me.jbgw("DB1.DBX0.2", True)
                        End If

                    End If
                End If
            Catch ex As Exception
                logfile.WriteError("1车间骨架板上料错误：" & ex.Message)
                MsgBox("读取RGV数据错误，关闭此信息后，会给接驳工位发信完成信号。" & vbCr & ex.Message)
                Me.jbgw("DB1.DBX0.2", True)
                Thread.Sleep(1000)
                Me.jbgw("DB1.DBX0.2", False)
                'tmGJBUPWait.Stop()
                pnlWait.Visible = False
                Exit While
            End Try
            Thread.Sleep(500)
        End While

    End Sub

    Private Sub tmFlbUpWait_Elapsed() 'sender As Object, e As ElapsedEventArgs) Handles tmFLBUPWait.Elapsed
        'pnlWait.BringToFront()
        'pnlWait.Visible = True
        'lblWait.Text = "等待上料完成"

        While True
            Try
                seterrInfo("等待上料完成" & vbCrLf & vbCrLf & " 完成前，不要关闭屏幕！！！！")
                UcRGV1._1下料完成 = _RGV_PLC.ReadBool("DB17.27.4").Content() '1#下料完成	Bool	DB17.27.4
                UcRGV1._2下料完成 = _RGV_PLC.ReadBool("DB17.27.3").Content() '2#下料完成	Bool	DB17.27.3
                UcRGV1._1上料完成 = _RGV_PLC.ReadBool("DB17.27.6").Content() '1#上料完成	Bool	DB17.27.6
                UcRGV1._2上料完成 = _RGV_PLC.ReadBool("DB17.27.7").Content() '2#上料完成	Bool	DB17.27.7
                UcRGV1.StopCmd = _RGV_PLC.ReadBool("DB16.4.4").Content()
                If Current_StationNo = 0 Then
                    If UcRGV1._1上料完成 Or UcRGV1.StopCmd Then 'UcRGV1.Stop_0 Or UcRGV1.StopCmd Then
                        logfile.WriteInfo("1车间法兰板空框上料完成Begin：滚轮" & Current_StationNo + 1)
                        'Me.jbgw("DB1.DBX0.0", False)
                        'Thread.Sleep(500)
                        If Me.jbgw("DB1.DBX0.2", True) Then
                            Thread.Sleep(1000)
                            Me.jbgw("DB1.DBX0.2", False)
                            pnlWait.Visible = False

                            logfile.WriteInfo("1车间法兰板空框上料完成end：滚轮" & Current_StationNo + 1)
                            Exit While
                        Else
                            logfile.WriteInfo("再次设定DB1.DBX0.2=true")
                            Me.jbgw("DB1.DBX0.2", True)
                        End If
                    End If
                Else
                    If UcRGV1._2上料完成 Or UcRGV1.StopCmd Then 'UcRGV1.Stop_1 Or UcRGV1.StopCmd Then
                        logfile.WriteInfo("1车间法兰板空框上料完成Begin：滚轮" & Current_StationNo + 1)
                        'Me.jbgw("DB1.DBX0.0", False)
                        'Thread.Sleep(500)
                        If Me.jbgw("DB1.DBX0.2", True) Then
                            Thread.Sleep(1000)
                            Me.jbgw("DB1.DBX0.2", False)
                            pnlWait.Visible = False
                            'tmFLBUPWait.Stop()
                            logfile.WriteInfo("1车间法兰板空框上料完成end：滚轮" & Current_StationNo + 1)
                            Exit While
                        Else
                            logfile.WriteInfo("再次设定DB1.DBX0.2=true")
                            Me.jbgw("DB1.DBX0.2", True)
                        End If
                    End If
                End If

            Catch ex As Exception
                logfile.WriteError("1车间法兰板上料错误：" & ex.Message)
                MsgBox("读取RGV数据错误，关闭此信息后，会给接驳工位发信完成信号。" & vbCr & ex.Message)
                Me.jbgw("DB1.DBX0.2", True)
                Thread.Sleep(1000)
                Me.jbgw("DB1.DBX0.2", False)
                pnlWait.Visible = False
                Exit While
            End Try
            Thread.Sleep(500)
        End While

    End Sub
    Private Sub tm2FlbUpWait_Elapsed() 'sender As Object, e As ElapsedEventArgs) Handles tm2FLBUPWait.Elapsed
        'pnlWait.BringToFront()
        'pnlWait.Visible = True
        'lblWait.Text = "等待上料完成"
        While True
            Try
                seterrInfo("等待上料完成" & vbCrLf & vbCrLf & " 完成前，不要关闭屏幕！！！！")
                'Me.readPLC()
                UcRGV1._1下料完成 = _RGV_PLC.ReadBool("DB17.27.4").Content() '1#下料完成	Bool	DB17.27.4
                UcRGV1._2下料完成 = _RGV_PLC.ReadBool("DB17.27.3").Content() '2#下料完成	Bool	DB17.27.3
                UcRGV1._1上料完成 = _RGV_PLC.ReadBool("DB17.27.6").Content() '1#上料完成	Bool	DB17.27.6
                UcRGV1._2上料完成 = _RGV_PLC.ReadBool("DB17.27.7").Content() '2#上料完成	Bool	DB17.27.7
                UcRGV1.StopCmd = _RGV_PLC.ReadBool("DB16.4.4").Content()
                If Current_StationNo = 0 Then
                    If UcRGV1._1上料完成 Or UcRGV1.StopCmd Then 'UcRGV1.Stop_0 Or UcRGV1.StopCmd Then
                        logfile.WriteInfo("2车间法兰板空框上料完成Begin：滚轮" & Current_StationNo + 1)
                        'Me.jbgw("DB200.DBX77.1", False)
                        'Thread.Sleep(500)
                        If Me.jbgw("DB200.DBX77.2", True) Then
                            Thread.Sleep(1000)
                            Me.jbgw("DB200.DBX77.2", False)
                            pnlWait.Visible = False
                            'tm2FLBUPWait.Stop()
                            logfile.WriteInfo("2车间法兰板空框上料完成end：滚轮" & Current_StationNo + 1)
                            Exit While
                        Else
                            logfile.WriteInfo("再次设定DB200.DBX77.2=true")
                            Me.jbgw("DB200.DBX77.2", True)
                        End If

                    End If
                Else
                    If UcRGV1._2上料完成 Or UcRGV1.StopCmd Then 'UcRGV1.Stop_1 Or UcRGV1.StopCmd Then
                        logfile.WriteInfo("2车间法兰板空框上料完成being：滚轮" & Current_StationNo + 1)
                        'Me.jbgw("DB200.DBX77.1", False)
                        'Thread.Sleep(500)
                        If Me.jbgw("DB200.DBX77.2", True) Then
                            Thread.Sleep(1000)
                            Me.jbgw("DB200.DBX77.2", False)
                            pnlWait.Visible = False
                            'tm2FLBUPWait.Stop()
                            logfile.WriteInfo("2车间法兰板空框上料完成end：滚轮" & Current_StationNo + 1)
                            Exit While
                        Else
                            logfile.WriteInfo("再次设定DB200.DBX77.2=true")
                            Me.jbgw("DB200.DBX77.2", True)
                        End If
                    End If
                End If
            Catch ex As Exception
                logfile.WriteError("2车间法兰板空框上料错误：" & ex.Message)
                MsgBox("读取RGV数据错误，关闭此信息后，会给接驳工位发信完成信号。" & vbCr & ex.Message)
                Me.jbgw("DB200.DBX77.2", True)
                Thread.Sleep(1000)
                Me.jbgw("DB200.DBX77.2", False)
                pnlWait.Visible = False
                'tm2FLBUPWait.Stop()
                Exit While
            End Try
            Thread.Sleep(500)
        End While
    End Sub

    Private Sub thDNWait_Elapsed() '下料线程
        Dim JBGW下料完成 As Boolean = False
        'Dim Car下料完成 As Boolean = False
        Dim Car_DN_CMD As String
        '远程下料完成1#	Bool	DB16.	7.4
        '远程下料完成2#	Bool	DB16.	7.5

        If Current_StationNo = 0 Then
            Car_DN_CMD = "DB16.7.4"
        Else
            Car_DN_CMD = "DB16.7.5"
        End If
        logfile.WriteInfo("等待下料完成")
        While True

            Try
                seterrInfo("等待下料完成" & vbCrLf & vbCrLf & " 完成前，不要关闭屏幕！！！！")
                JBGW下料完成 = _JBGW_PLC.ReadBool(DN_CMD).Content() '1#下料完成	Bool	DB17.27.4
                'Car下料完成 = _RGV_PLC.ReadBool(Car_DN_CMD).Content()

                If JBGW下料完成 Then
                    logfile.WriteInfo("接驳工位下料完成")
                    If _RGV_PLC.Write(Car_DN_CMD, True).IsSuccess Then
                        logfile.WriteInfo("发给RGV下料完成信号True 完成")
                    Else
                        logfile.WriteInfo("发给RGV下料完成信号True 失败")
                    End If
                    Thread.Sleep(500)
                    If _RGV_PLC.Write(Car_DN_CMD, False).IsSuccess Then
                        logfile.WriteInfo("发给RGV下料完成信号False 完成")
                    Else
                        logfile.WriteInfo("发给RGV下料完成信号False 失败")
                    End If
                    pnlWait.Visible = False
                    Exit While
                End If

            Catch ex As Exception
                logfile.WriteError("下料错误：" & ex.Message)
                MsgBox("读取RGV数据错误。" & vbCr & ex.Message)
                pnlWait.Visible = False
                Exit While
            End Try
            Thread.Sleep(500)
        End While
    End Sub
    Private Sub tm2GJBUpWait_Elapsed() 'sender As Object, e As ElapsedEventArgs) Handles tm2GJBUPWait.Elapsed
        'pnlWait.BringToFront()
        'pnlWait.Visible = True
        'lblWait.Text = "等待上料完成"
        logfile.WriteInfo("开始 监控上料----------------------------")
        While True

            Try
                seterrInfo("等待上料完成" & vbCrLf & vbCrLf & " 完成前，不要关闭屏幕！！！！")
                Thread.Sleep(500)

                UcRGV1._1下料完成 = _RGV_PLC.ReadBool("DB17.27.4").Content() '1#下料完成	Bool	DB17.27.4
                UcRGV1._2下料完成 = _RGV_PLC.ReadBool("DB17.27.3").Content() '2#下料完成	Bool	DB17.27.3
                UcRGV1._1上料完成 = _RGV_PLC.ReadBool("DB17.27.6").Content() '1#上料完成	Bool	DB17.27.6
                UcRGV1._2上料完成 = _RGV_PLC.ReadBool("DB17.27.7").Content() '2#上料完成	Bool	DB17.27.7
                UcRGV1.StopCmd = _RGV_PLC.ReadBool("DB16.4.4").Content()
                Dim Rollstop As Boolean = getCarRollerStopStatus(Convert.ToString(_RGV_PLC.ReadInt32("DB17.8.0").Content, 2), Current_StationNo)
                If Current_StationNo = 0 Then

                    If UcRGV1._1上料完成 Or UcRGV1.StopCmd Or Rollstop Then 'UcRGV1.Stop_0 Or UcRGV1.StopCmd Then
                        logfile.WriteInfo(String.Format("2车间骨架板空框上料完成begin：滚轮{0} _1上料完成:{1},StopCmd:{2},Rollstop:{3} ", Current_StationNo + 1, UcRGV1._1上料完成, UcRGV1.StopCmd, Rollstop))
                        'Me.jbgw("DB200.DBX77.4", False)
                        'Thread.Sleep(500)
                        If Me.jbgw("DB200.DBX77.5", True) Then
                            Thread.Sleep(500)
                            If _JBGW_PLC.ReadBool("DB200.DBX121.2").Content Then
                                logfile.WriteInfo("DB200.DBX121.2=true")
                                Thread.Sleep(1000)
                                Me.jbgw("DB200.DBX77.5", False)
                                pnlWait.Visible = False
                                'tm2GJBUPWait.Stop()
                                logfile.WriteInfo("2车间骨架板空框上料完成end：滚轮" & Current_StationNo + 1)
                                Exit While
                            Else
                                logfile.WriteInfo("DB200.DBX121.2=false,再次设定DB200.DBX77.5=true")
                                Me.jbgw("DB200.DBX77.5", True)
                            End If
                        Else
                            logfile.WriteInfo("2车间骨架板空框上料 77.5 设置出错")
                        End If
                    End If
                Else
                    If UcRGV1._2上料完成 Or UcRGV1.StopCmd Or Rollstop Then 'UcRGV1.Stop_1 Or UcRGV1.StopCmd Then
                        'Me.jbgw("DB200.DBX77.4", False)
                        'Thread.Sleep(500)
                        logfile.WriteInfo(String.Format("2车间骨架板空框上料完成begin：滚轮{0} _1上料完成:{1},StopCmd:{2},Rollstop:{3} ", Current_StationNo + 1, UcRGV1._2上料完成, UcRGV1.StopCmd, Rollstop))

                        If Me.jbgw("DB200.DBX77.5", True) Then
                            Thread.Sleep(500)
                            If _JBGW_PLC.ReadBool("DB200.DBX121.2").Content Then
                                logfile.WriteInfo("DB200.DBX121.2=true")
                                Thread.Sleep(1000)
                                Me.jbgw("DB200.DBX77.5", False)
                                pnlWait.Visible = False
                                'tm2GJBUPWait.Stop()
                                logfile.WriteInfo("2车间骨架板空框上料完成end：滚轮" & Current_StationNo + 1)
                                Exit While
                            Else
                                logfile.WriteInfo("DB200.DBX121.2=false,再次设定DB200.DBX77.5=true")
                                Me.jbgw("DB200.DBX77.5", True)
                            End If
                        Else
                            logfile.WriteInfo("2车间骨架板空框上料 77.5 设置出错")
                        End If
                    End If
                End If
            Catch ex As Exception
                logfile.WriteError("2车间骨架板空框上料错误：" & ex.Message)
                MsgBox("读取RGV数据错误，关闭此信息后，会给接驳工位发信完成信号。" & vbCr & ex.Message)
                Me.jbgw("DB200.DBX77.5", True)
                Thread.Sleep(1000)
                Me.jbgw("DB200.DBX77.5", False)
                pnlWait.Visible = False
                'tm2GJBUPWait.Stop()
                Exit While
            End Try

        End While
        logfile.WriteInfo("结束 监控上料----------------------------")
    End Sub
    Private Function jbgwConn() As Boolean
        'Dim ipAddress As String = "127.0.0.1"
        Select Case lblMdd.Text
            Case "1FKX", "1FMS"
                'ipAddress =' GetIniFile("JBGW", "1F", "", Me.g_szInitFileName)
                _JBGW_PLC = _1F_JBGW_PLC
                Return _1F_Connected
            Case "1GMS-3", "1GKX-3"
                'ipAddress = 'GetIniFile("JBGW", "1G", "", Me.g_szInitFileName)
                _JBGW_PLC = _1G3_JBGW_PLC
                Return _1G3_Connected
            Case "1GMS1-1", "1GMS1-2", "1GKX-1"
                'ipAddress =' GetIniFile("JBGW", "1G2", "", Me.g_szInitFileName)
                _JBGW_PLC = _1G2_JBGW_PLC
                Return _1G2_Connected
            Case "2FMX", "2FKS", "2GMX", "2GKS"
                _JBGW_PLC = _2_JBGW_PLC
                Return _2_Connected
                'ipAddress =' GetIniFile("JBGW", "2CJ", "", Me.g_szInitFileName)
            Case Else
                Return False
        End Select

    End Function

    Private Function jbgw(_cmd As String, bln As Boolean) As Boolean
        Dim _result As OperateResult = _JBGW_PLC.Write(_cmd, bln)

        logfile.WriteInfo(String.Format("写入接驳工位：{0}={1}{2}{3}", _cmd, bln, _result.IsSuccess, _result.Message))
        Return _result.IsSuccess
    End Function

    Private Sub frmRGVcontrol_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.Closing
        Dim connectSuccess As Boolean = connectSuccess

        If connectSuccess Then
            Me.disconnect()
        End If
        Try
            If RGV_READ_FLG = True Then
                RGV_READ_FLG = False
            End If
            If RGV_READ_TH.IsAlive Then
                RGV_READ_TH.Abort()
            End If
        Catch ex As Exception

        End Try

        timer2.Stop()
        _monitor = False
        End
        'Try
        '    If th.IsAlive Then th.Abort()
        'Catch ex As Exception

        'End Try

    End Sub

    Private Sub UserSwitch1_Click(sender As Object, e As EventArgs) Handles swhDoor.Click
        Dim array As String() = swhDoor.Tag.ToString().Split("@")

        If _connectSuccess Then

            If swhDoor.SwitchStatus() Then
                Me.send(Me.active_DB + array(1), True)
            Else
                Me.send(Me.active_DB + array(0), True)
            End If
        Else
            swhDoor.SwitchStatus = Not swhDoor.SwitchStatus
        End If
    End Sub

    Public Sub connectDB()
        'イニシャルファイル情報取得

        g_Server.szDrive = GetIniFile("SERVER", "DRIVE", "PostgreSQL ANSI", g_szInitFileName)
        g_Server.szIPadr = GetIniFile("SERVER", "IP", "", g_szInitFileName)
        g_Server.szDbn = GetIniFile("SERVER", "DBN", "ZHENANSF", g_szInitFileName)
        g_Server.szUid = GetIniFile("SERVER", "UID", "sa", g_szInitFileName)
        g_Server.szPas = GetIniFile("SERVER", "PAS", "sa", g_szInitFileName)
        g_Server.iOver = CInt(GetIniFile("SERVER", "OVER", "-1", g_szInitFileName))

        '得到当前用户名
        'g_LoginUser = GetIniFile("LOGIN", "user", "admin", g_szInitFileName)

        If False = g_fDBconnect And 0 = g_iDBconnResult Then
            If "" = g_Server.szIPadr Or "0.0.0.0" = g_Server.szIPadr Then
            Else
                g_fDBconnect = True

                'データベース接続
                With g_Server
                    g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    If 1 <> g_iDBconnResult Then
                        .szDrive = CStr(IIf("PostgreSQL" = .szDrive, "PostgreSQL ANSI", "PostgreSQL"))
                        g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    End If
                End With
            End If
        End If

        setAllErrMsg()
    End Sub
    Dim _caller1F As New setJBGW_StatusDelegate(AddressOf setJBGW_Status)

    'Case "1GMS-3", "1GKX-3"
    Dim _caller_1G3 As New setJBGW_StatusDelegate(AddressOf setJBGW_Status)

    'Case "1GMS1-1", "1GMS1-2", "1GKX-1"
    Dim _caller_1G2 As New setJBGW_StatusDelegate(AddressOf setJBGW_Status)

    'Case "2FMX", "2FKS", "2GMX", "2GKS"
    Dim _caller_2 As New setJBGW_StatusDelegate(AddressOf setJBGW_Status)
    Private Sub active()

        Try
            Dim ipAddress1F As String = GetIniFile("JBGW", "1F", "", Me.g_szInitFileName)
            Dim _caller1F As New setJBGW_StatusDelegate(AddressOf setJBGW_Status)

            'Case "1GMS-3", "1GKX-3"
            Dim ipAddress1G3 As String = GetIniFile("JBGW", "1G", "", Me.g_szInitFileName)
            Dim _caller_1G3 As New setJBGW_StatusDelegate(AddressOf setJBGW_Status)

            'Case "1GMS1-1", "1GMS1-2", "1GKX-1"
            Dim ipAddress1G2 As String = GetIniFile("JBGW", "1G2", "", Me.g_szInitFileName)
            Dim _caller_1G2 As New setJBGW_StatusDelegate(AddressOf setJBGW_Status)

            'Case "2FMX", "2FKS", "2GMX", "2GKS"
            Dim ipAddress2 As String = GetIniFile("JBGW", "2CJ", "", Me.g_szInitFileName)
            Dim _caller_2 As New setJBGW_StatusDelegate(AddressOf setJBGW_Status)
            '==========================连接4个接驳工位PLC==================================
            _1F_JBGW_PLC = New SiemensS7Net(SiemensPLCS.S1200)
            'End Select
            With _1F_JBGW_PLC
                .IpAddress = ipAddress1F
                _1F_JBGW_PLC.Port = 102
                .SetPersistentConnection()
                '_1F_Connected = .ConnectServer().IsSuccess
                'Me.Invoke(_caller1F, 1, _1F_Connected)
            End With

            _1G2_JBGW_PLC = New SiemensS7Net(SiemensPLCS.S1200)
            With _1G2_JBGW_PLC
                .IpAddress = ipAddress1G2
                _1G2_JBGW_PLC.Port = 102
                .SetPersistentConnection()
                '_1G2_Connected = .ConnectServer().IsSuccess
                'Me.Invoke(_caller_1G2, 2, _1G2_Connected)
            End With
            _1G3_JBGW_PLC = New SiemensS7Net(SiemensPLCS.S1200)
            With _1G3_JBGW_PLC
                .IpAddress = ipAddress1G3
                _1G3_JBGW_PLC.Port = 102
                .SetPersistentConnection()

                '_1G3_Connected = .ConnectServer().IsSuccess
                'Me.Invoke(_caller_1G3, 3, _1G3_Connected)
            End With
            _2_JBGW_PLC = New SiemensS7Net(SiemensPLCS.S1200)
            With _2_JBGW_PLC
                .IpAddress = ipAddress2
                _2_JBGW_PLC.Port = 102
                .SetPersistentConnection()
                '_2_Connected = .ConnectServer().IsSuccess
                'Me.Invoke(_caller_2, 4, _2_Connected)
            End With
            '=============================================================================
            '=======================监听4个工位是否能联通=================================
            While _monitor
                If _1F_JBGW_PLC.ReadBool("DB1.DBX0.0").IsSuccess Then
                    Me.Invoke(_caller1F, 1, True)
                    _1F_Connected = True
                Else
                    Me.Invoke(_caller1F, 1, False)
                    _1F_Connected = False
                End If

                If _1G2_JBGW_PLC.ReadBool("DB15.DBX0.0").IsSuccess Then
                    Me.Invoke(_caller_1G2, 2, True)
                    _1G2_Connected = True
                Else
                    Me.Invoke(_caller_1G2, 2, False)
                    _1G2_Connected = False
                End If

                If _1G3_JBGW_PLC.ReadBool("DB1.DBX0.0").IsSuccess Then
                    Me.Invoke(_caller_1G3, 3, True)
                    _1G3_Connected = True
                Else
                    Me.Invoke(_caller_1G3, 3, False)
                    _1G3_Connected = False
                End If

                If _2_JBGW_PLC.ReadBool("DB200.DBX112.0").IsSuccess Then
                    Me.Invoke(_caller_2, 4, True)
                    _2_Connected = True
                Else
                    Me.Invoke(_caller_2, 4, False)
                    _2_Connected = False
                End If
                JBGW_stop_status()
                Thread.Sleep(1000)
            End While
        Catch ex As Exception

        End Try
    End Sub
    Private Sub JBGW_stop_status()
        '-----------------------------------------------
        If _1F_Connected Then
            If _1F_JBGW_PLC.ReadBool("DB1.DBX0.4").Content Then
                lblStop1F.Visible = True
                lblStop1F.Text = "M急停"
            End If
            If _1F_JBGW_PLC.ReadBool("DB1.DBX0.5").Content Then
                lblStop1F.Visible = True
                lblStop1F.Text = "急停拍下"
                lblStop1F.Refresh()
            End If
            If _1F_JBGW_PLC.ReadBool("DB1.DBX0.4").Content = False And _1F_JBGW_PLC.ReadBool("DB1.DBX0.5").Content = False Then
                lblStop1F.Visible = False
            End If
        Else
            lblStop1F.Visible = False
        End If
        '-----------------------------------------------
        If _1G2_Connected Then
            If _1G2_JBGW_PLC.ReadBool("DB15.DBX0.6").Content Then
                lblStop1G2.Visible = True
                lblStop1G2.Text = "M急停"
            End If
            If _1G2_JBGW_PLC.ReadBool("DB15.DBX0.7").Content Then
                lblStop1G2.Visible = True
                lblStop1G2.Text = "急停拍下"
                lblStop1G2.Refresh()
            End If
            If _1G2_JBGW_PLC.ReadBool("DB15.DBX0.6").Content = False And _1G2_JBGW_PLC.ReadBool("DB15.DBX0.7").Content = False Then
                lblStop1G2.Visible = False
            End If
        Else
            lblStop1G2.Visible = False
        End If
        '-----------------------------------------------
        If _1G3_Connected Then
            If _1G3_JBGW_PLC.ReadBool("DB1.DBX0.4").Content Then
                lblStop1G3.Visible = True
                lblStop1G3.Text = "M急停"
            End If
            If _1G3_JBGW_PLC.ReadBool("DB1.DBX0.5").Content Then
                lblStop1G3.Visible = True
                lblStop1G3.Text = "急停拍下"
                lblStop1G3.Refresh()
            End If
            If _1G3_JBGW_PLC.ReadBool("DB1.DBX0.4").Content = False And _1G3_JBGW_PLC.ReadBool("DB1.DBX0.5").Content = False Then
                lblStop1G3.Visible = False
            End If
        Else
            lblStop1G3.Visible = False
        End If
        '-----------------------------------------------
        If _2_Connected Then

            If _2_JBGW_PLC.ReadBool("DB200.DBX111.3").Content Then
                lblStop2F.Visible = True
                lblStop2F.Text = "急停拍下"
                lblStop2F.Refresh()
            End If
            If _2_JBGW_PLC.ReadBool("DB200.DBX111.3").Content = False Then 'And _1G3_JBGW_PLC.ReadBool("DB1.DBX0.5").Content = False Then
                lblStop2F.Visible = False
            End If
            '-----------------------------------------------
            If _2_JBGW_PLC.ReadBool("DB200.DBX111.6").Content Then
                lblStop2G.Visible = True
                lblStop2G.Text = "急停拍下"
                lblStop2G.Refresh()
            End If
            If _2_JBGW_PLC.ReadBool("DB200.DBX111.6").Content = False Then 'And _1G3_JBGW_PLC.ReadBool("DB1.DBX0.5").Content = False Then
                lblStop2G.Visible = False
            End If
        Else
            lblStop2F.Visible = False
            lblStop2G.Visible = False
        End If

    End Sub

    Delegate Sub setJBGW_StatusDelegate(intFlg As Integer, bln As Boolean)

    Private Sub setJBGW_Status(intFlg As Integer, bln As Boolean)
        Select Case intFlg
            Case 1
                If bln = True Then
                    pnl1F.BackColor = Color.GreenYellow
                Else
                    pnl1F.BackColor = Color.Red
                End If

            Case 2
                'IIf(bln = True, pnl1G2.BackColor = Color.GreenYellow, pnl1G2.BackColor = Color.Red)
                If bln = True Then
                    pnl1G2.BackColor = Color.GreenYellow
                Else
                    pnl1G2.BackColor = Color.Red
                End If
            Case 3
                'IIf(bln = True, pnl1G3.BackColor = Color.GreenYellow, pnl1G3.BackColor = Color.Red)
                If bln = True Then
                    pnl1G3.BackColor = Color.GreenYellow
                Else
                    pnl1G3.BackColor = Color.Red
                End If
            Case 4
                'IIf(bln = True, pnl2.BackColor = Color.GreenYellow, pnl2.BackColor = Color.Red)
                If bln = True Then
                    pnl2.BackColor = Color.GreenYellow
                Else
                    pnl2.BackColor = Color.Red
                End If
        End Select
        'Me.Refresh()
    End Sub

    Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
        If _connectSuccess Then
            UcRGV1.StopCmd = Not btnStop.Selected()
            Me.send2("DB16.4.4", Not btnStop.Selected())
        End If

        If chkZS.Checked Then
            Dim _cmd As String = ""

            Select Case lblMdd.Text
                Case "1FKX", "1FMS", "1GMS-3", "1GKX-3"
                    _cmd = "DB1.DBX0.4"
                Case "1GMS1-1", "1GMS1-2", "1GKX-1"
                    _cmd = "DB15.DBX0.6"
                Case "2FMX", "2FKS"
                    _cmd = "DB200.DBX111.3"
                Case "2GMX", "2GKS"
                    _cmd = "DB200.DBX111.6"
            End Select
            If _cmd <> "" Then
                If jbgwConn() = False Then
                    seterrInfo("接驳工位 连接失败，无法启动。")
                    'Exit Sub
                Else
                    Me.jbgw(_cmd, Not btnStop.Selected())
                End If
            End If

        End If

        btnStop.Selected = Not btnStop.Selected()

        If btnStop.Selected() Then
            btnStop.Text = "急  停  中"
            UcRGV1.lanStop.Visible = True
        Else
            btnStop.Text = "急  停"
            UcRGV1.lanStop.Visible = False
        End If

    End Sub

    Private Sub RadioButton6_CheckedChanged(sender As Object, e As EventArgs)
        lblMdd.Text = "CDW"
        lblFGFlg.Text = sender.tag
    End Sub

#Region "RGV各种状态"

    Sub setOff(_name As String)
        For Each obj As Control In pnlManual.Controls
            If obj.Name <> _name Then
                Dim a = CType(obj, UserButton)
                a.Selected = False
            End If
        Next
    End Sub
    Private Sub UserButton1_Click(sender As Object, e As EventArgs) Handles UserButton1.Click
        setOff(sender.name)
        sender.selected = Not sender.selected
        UcRGV1.M_11挡板升 = sender.selected
    End Sub

    Private Sub UserButton2_Click(sender As Object, e As EventArgs) Handles UserButton2.Click
        setOff(sender.name)
        sender.selected = Not sender.selected
        UcRGV1.M_11挡板降 = sender.selected
    End Sub

    Private Sub UserButton3_Click(sender As Object, e As EventArgs) Handles UserButton3.Click
        setOff(sender.name)
        sender.selected = Not sender.selected
        UcRGV1.M_12挡板升 = sender.selected
    End Sub

    Private Sub UserButton4_Click(sender As Object, e As EventArgs) Handles UserButton4.Click
        setOff(sender.name)
        sender.selected = Not sender.selected
        UcRGV1.M_12挡板降 = sender.selected
    End Sub

    Private Sub UserButton6_Click(sender As Object, e As EventArgs) Handles UserButton6.Click
        setOff(sender.name)
        sender.selected = Not sender.selected
        UcRGV1.M_21挡板升 = sender.selected
    End Sub

    Private Sub UserButton5_Click(sender As Object, e As EventArgs) Handles UserButton5.Click
        setOff(sender.name)
        sender.selected = Not sender.selected
        UcRGV1.M_21挡板降 = sender.selected
    End Sub

    Private Sub UserButton8_Click(sender As Object, e As EventArgs) Handles UserButton8.Click
        setOff(sender.name)
        sender.selected = Not sender.selected
        UcRGV1.M_22挡板升 = sender.selected
    End Sub

    Private Sub UserButton7_Click(sender As Object, e As EventArgs) Handles UserButton7.Click
        setOff(sender.name)
        sender.selected = Not sender.selected
        UcRGV1.M_22挡板降 = sender.selected
    End Sub

    Private Sub UserButton10_Click(sender As Object, e As EventArgs) Handles UserButton10.Click
        setOff(sender.name)
        sender.selected = Not sender.selected
        UcRGV1.M_1滚轮正转 = sender.selected
    End Sub

    Private Sub UserButton9_Click(sender As Object, e As EventArgs) Handles UserButton9.Click
        setOff(sender.name)
        sender.selected = Not sender.selected
        UcRGV1.M_1滚轮反转 = sender.selected
    End Sub

    Private Sub UserButton12_Click(sender As Object, e As EventArgs) Handles UserButton12.Click
        setOff(sender.name)
        sender.selected = Not sender.selected
        UcRGV1.M_2滚轮正转 = sender.selected
    End Sub

    Private Sub UserButton11_Click(sender As Object, e As EventArgs) Handles UserButton11.Click
        setOff(sender.name)
        sender.selected = Not sender.selected
        UcRGV1.M_2滚轮反转 = sender.selected
    End Sub

    'Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As EventArgs)
    '    If TabControl1.SelectedIndex = 0 Then
    '        'th.Start()
    '        If timer2.Enabled = False Then
    '            timer2.Start()
    '        End If
    '    Else
    '        'If th.IsAlive Then
    '        '    _monitor = False
    '        '    th.Abort()
    '        'End If
    '        If timer2.Enabled = True Then
    '            timer2.Stop()
    '        End If
    '    End If

    '    UcRGV1.手动自动 = TabControl1.SelectedIndex
    'End Sub
#End Region

    Private Sub rdo1_CheckedChanged(sender As Object, e As EventArgs) Handles rdo6.CheckedChanged, rdo5.CheckedChanged, rdo4.CheckedChanged, rdo3.CheckedChanged, rdo2.CheckedChanged, rdo1.CheckedChanged, RadioButton6.CheckedChanged, RadioButton5.CheckedChanged, RadioButton4.CheckedChanged, RadioButton3.CheckedChanged, RadioButton2.CheckedChanged, RadioButton1.CheckedChanged
        lblMdd.Text = sender.text
        lblFGFlg.Text = sender.tag
    End Sub

    Public Sub connect(_IP As String)
        'If _connectSuccess Then
        '    Me._RGV_PLC.ConnectClose()
        '    _connectSuccess = False
        'End If
        _RGV_PLC = New SiemensS7Net(SiemensPLCS.S1200)
        _RGV_PLC.IpAddress = _IP
        _RGV_PLC.Port = 102
        _RGV_PLC.ConnectTimeOut = 5000
        _RGV_PLC.SetPersistentConnection()
        ConnectSuccess = _RGV_PLC.ReadBool("DB17.26.0").IsSuccess
        If ConnectSuccess = True Then
            lblconnect.Text = "连接成功！"
            lblconnect.BackColor = Color.Green
            lblconnect.ForeColor = Color.White
        End If
        'Me.readPLC()
    End Sub

    Public Sub disconnect()
        If _connectSuccess Then
            Me._RGV_PLC.ConnectClose()
            ConnectSuccess = False
        End If
    End Sub

    Public Function send(_address As String, _value As Boolean) As Boolean
        Dim result As Boolean = False
        If _connectSuccess Then
            If _RGV_PLC.Write(_address, _value).IsSuccess() Then
                logfile.WriteInfo(String.Format("RGV【{0}】={1}", _address, _value))
                Thread.Sleep(500)
                Me._RGV_PLC.Write(_address, Not _value)
                result = True
            Else
                logfile.WriteError(String.Format("写入失败！RGV【{0}】={1}", _address, _value))
                seterrInfo("写入失败！地址：" + _address + "值：" + _value)
                result = False
            End If
        End If
        Return result
    End Function

    Public Function send2(_address As String, _value As Boolean) As Boolean
        Dim result As Boolean = False

        If _connectSuccess Then
            If _RGV_PLC.Write(_address, _value).IsSuccess() Then
                result = True
            Else
                seterrInfo("写入失败！地址：" + _address + "值：" + _value)
                result = False
            End If
        End If
        Return result
    End Function

    Private Sub readPLC()
        While RGV_READ_FLG
            If _connectSuccess Then
                Dim isSuccess As Boolean = _RGV_PLC.ReadBool("DB17.26.0").IsSuccess()
                If isSuccess Then

                    _heard = Not _heard
                    _RGV_PLC.Write("DB16.4.3", Me._heard)
                    _Heard_Num = _Heard_Num + 1
                    logfileHeard.WriteInfo(_Current_Car_Name & ":" & _Heard_Num)
                    UcRGV1.ControlMode = _RGV_PLC.ReadBool("DB17.27.5").Content()
                    UcRGV1.Battery.Value = _RGV_PLC.ReadFloat("DB17.20.0").Content()
                    UcRGV1.DoorOpen = _RGV_PLC.ReadBool("DB17.26.2").Content()
                    UcRGV1.MapNo = _RGV_PLC.ReadInt32("DB17.4.0").Content()
                    'UcRGV1.CarStop = IIf(_RGV_PLC.ReadBool("DB17.27.2").Content() Or _RGV_PLC.ReadBool("DB16.4.4").Content(), True, False)
                    UcRGV1._1下料完成 = _RGV_PLC.ReadBool("DB17.27.4").Content() '1#下料完成	Bool	DB17.27.4
                    UcRGV1._2下料完成 = _RGV_PLC.ReadBool("DB17.27.3").Content() '2#下料完成	Bool	DB17.27.3
                    UcRGV1._1上料完成 = _RGV_PLC.ReadBool("DB17.27.6").Content() '1#上料完成	Bool	DB17.27.6
                    UcRGV1._2上料完成 = _RGV_PLC.ReadBool("DB17.27.7").Content() '2#上料完成	Bool	DB17.27.7

                    'Dim stopCmd As Boolean = UcRGV1.StopCmd
                    'If stopCmd Then
                    '    Me._RGV_PLC.Write("DB16.4.4", True)
                    '    'lblDD.Visible = False
                    'Else
                    '    'lblDD.Visible = True
                    'End If

                    Dim arriveResult As OperateResult(Of Integer) = _RGV_PLC.ReadInt32("DB17.4.0")
                    If arriveResult.IsSuccess = True Then

                        If arriveResult.Content = 0 Then
                            'lblDD.Text = "<- RGV移动中.."
                            'lblDD.Refresh()
                        End If

                        If arriveResult.Content = _Current_MDD And arriveResult.Content <> 0 Then

                            lblDD.Text = "<-到达"
                            lblDD.Refresh()
                        End If
                    Else
                        lblDD.Text = "目的地读取失败。尝试重新读取中..."
                        lblDD.Refresh()
                    End If

                    Select Case _RGV_PLC.ReadInt16("DB17.2.0").Content()
                        Case 0
                            UcRGV1.lblCarMove.Text = "静止"
                        Case 1
                            UcRGV1.lblCarMove.Text = "低速前进中..."
                        Case 2
                            UcRGV1.lblCarMove.Text = "高速前进中..."
                        Case 3
                            UcRGV1.lblCarMove.Text = "低速后退中..."
                        Case 4
                            UcRGV1.lblCarMove.Text = "高速后退中..."
                    End Select

                    '开关车间门
                    '1#开门信号	Bool	26.5
                    'If _RGV_PLC.ReadBool("DB17.26.5").Content = True Then
                    '    If MsgBox("确认 1车间门 是否打开?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                    '        '1#门上升到位信号	Bool	5.1
                    '        _RGV_PLC.Write("DB16.5.1", True)
                    '    End If
                    'End If
                    ''1#关门信号	Bool	26.6
                    ''2#开门信号	Bool	26.7
                    'If _RGV_PLC.ReadBool("DB17.26.7").Content = True Then
                    '    If MsgBox("确认 2车间门 是否打开?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                    '        '2#门上升到位信号	Bool	5.2
                    '        _RGV_PLC.Write("DB16.5.2", True)
                    '    End If
                    'End If
                    '2#关门信号	Bool	27.0

                    _RGV_PLC.Write("DB16.5.5", IIf(UcRGV1.手动自动 = 1, True, False))
                    If Me.UcRGV1.手动自动 = 1 Then
                        Me._RGV_PLC.Write("DB16.6.0", UcRGV1.M_11挡板升)
                        Me._RGV_PLC.Write("DB16.6.1", UcRGV1.M_11挡板降)
                        Me._RGV_PLC.Write("DB16.6.2", UcRGV1.M_12挡板升)
                        Me._RGV_PLC.Write("DB16.6.3", UcRGV1.M_12挡板降)
                        Me._RGV_PLC.Write("DB16.6.4", UcRGV1.M_21挡板升)
                        Me._RGV_PLC.Write("DB16.6.5", UcRGV1.M_21挡板降)
                        Me._RGV_PLC.Write("DB16.6.6", UcRGV1.M_22挡板升)
                        Me._RGV_PLC.Write("DB16.6.7", UcRGV1.M_22挡板降)
                        Me._RGV_PLC.Write("DB16.7.0", UcRGV1.M_1滚轮正转)
                        Me._RGV_PLC.Write("DB16.7.1", UcRGV1.M_1滚轮反转)
                        Me._RGV_PLC.Write("DB16.7.2", UcRGV1.M_2滚轮正转)
                        Me._RGV_PLC.Write("DB16.7.3", UcRGV1.M_2滚轮反转)
                    End If
                    Me.setCarStatus(Convert.ToString(_RGV_PLC.ReadInt32("DB17.8.0").Content, 2))
                Else
                    logfileHeard.WriteInfo(_Current_Car_Name & ":" & "失去链接1")
                    _connectSuccess = False
                    _RGV_PLC.ConnectClose()
                    lblconnect.Text = "失去连接,请点击[连接RGV]再次尝试。"
                    lblconnect.BackColor = Color.Red
                    lblconnect.ForeColor = Color.White
                    btnConnect.Text = "连接RGV"
                    'Dim _ip As String
                    'If rdoCar1.Checked Then
                    '    _ip = car1Ip
                    '    Me.connect(_ip)
                    'End If

                    'If rdoCar2.Checked Then
                    '    _ip = car2Ip
                    '    Me.connect(_ip)
                    'End If
                End If
            Else
                logfileHeard.WriteInfo(_Current_Car_Name & ":" & "失去链接2")
                _connectSuccess = False
                _RGV_PLC.ConnectClose()
                lblconnect.Text = "失去连接,请点击[连接RGV]再次尝试。"
                lblconnect.BackColor = Color.Red
                lblconnect.ForeColor = Color.White
                btnConnect.Text = "连接RGV"
                'Dim _ip As String
                'If rdoCar1.Checked Then
                '    _ip = car1Ip
                '    Me.connect(_ip)
                'End If

                'If rdoCar2.Checked Then
                '    _ip = car2Ip
                '    Me.connect(_ip)
                'End If

            End If
            Thread.Sleep(500)
        End While
    End Sub

    Public Sub setCarStatus(recStr As String)

        If recStr <> "" Then
            recStr = recStr.PadLeft(32, "0")
            Dim i As Integer = 2
            Dim array As Integer() = New Integer(16 - 1) {}
            Dim num As Integer = 0
            While i < 34
                array(num) = recStr.Substring(32 - i, 2)
                i += 2
                num += 1
            End While

            If array(0) = 10 Then
                UcRGV1.MidPnl_1.Height = 3 '降
            Else
                UcRGV1.MidPnl_1.Height = 33 '升
            End If

            If array(1) = 10 Then
                UcRGV1.extPnl_1.Height = 3 '降
            Else
                UcRGV1.extPnl_1.Height = 33 '升
            End If

            If array(2) = 10 Then
                UcRGV1.MidPnl_2.Height = 3 '降
            Else
                UcRGV1.MidPnl_2.Height = 33 '升
            End If

            If array(3) = 10 Then
                UcRGV1.extPnl_2.Height = 3 '降
            Else
                UcRGV1.extPnl_2.Height = 33 '升
            End If

            If array(4) = 10 Or array(4) = 0 Then
                UcRGV1.WPnl_1.BackColor = Color.White '无货
            Else
                UcRGV1.WPnl_1.BackColor = Color.Black '有货
            End If

            If array(5) = 10 Or array(5) = 0 Then
                UcRGV1.Npnl_1.BackColor = Color.White '无货
            Else
                UcRGV1.Npnl_1.BackColor = Color.Black '有货
            End If

            If array(6) = 10 Or array(6) = 0 Then
                UcRGV1.WPnl_2.BackColor = Color.White '无货
            Else
                UcRGV1.WPnl_2.BackColor = Color.Black '有货
            End If

            If array(7) = 10 Or array(7) = 0 Then
                UcRGV1.Npnl_2.BackColor = Color.White '无货
            Else
                UcRGV1.Npnl_2.BackColor = Color.Black '有货
            End If

            If array(8) = 10 Then
                UcRGV1.upPipe1.MoveSpeed = -1
                UcRGV1.upPipe2.MoveSpeed = -1
                UcRGV1.Stop_0 = False
            Else
                If array(8) = 1 Then
                    UcRGV1.upPipe1.MoveSpeed = 1
                    UcRGV1.upPipe2.MoveSpeed = 1
                    UcRGV1.Stop_0 = False
                End If
            End If

            If array(9) = 10 Then
                UcRGV1.dnPipe1.MoveSpeed = -1
                UcRGV1.dnPipe2.MoveSpeed = -1
                UcRGV1.Stop_1 = False
            Else
                If array(9) = 1 Then
                    UcRGV1.dnPipe1.MoveSpeed = 1
                    UcRGV1.dnPipe2.MoveSpeed = 1
                    UcRGV1.Stop_1 = False
                End If
            End If

            If array(10).ToString().PadLeft(2, "0").Substring(1, 1) = "1" Then
                Me.UcRGV1.upPipe1.MoveSpeed = 0 '停
                Me.UcRGV1.upPipe2.MoveSpeed = 0 '停
                Me.UcRGV1.Stop_0 = True
            End If

            If array(10).ToString().PadLeft(2, "0").Substring(0, 1) = "1" Then
                Me.UcRGV1.dnPipe1.MoveSpeed = 0 '停
                Me.UcRGV1.dnPipe2.MoveSpeed = 0 '停
                Me.UcRGV1.Stop_1 = True
            End If
        End If
    End Sub

    'Private Sub timer1_Elapsed(sender As Object, e As ElapsedEventArgs) Handles timer1.Elapsed
    '    Me.readPLC()
    'End Sub

    Private Sub frmRGVcontrol_Closed(sender As Object, e As EventArgs) Handles Me.Closed
        'Dim isAlive As Boolean = Me.th.IsAlive
        'If isAlive Then
        '    Me.th.Abort()
        'End If

        If IsNothing(Me._RGV_PLC) = False Then
            Me._RGV_PLC.ConnectClose()
        End If

        If IsNothing(_JBGW_PLC) = False Then
            Me._JBGW_PLC.ConnectClose()
        End If
    End Sub
    Private Sub btnSizeSetting_Click(sender As Object, e As EventArgs) Handles btnSizeSetting.Click
        If cmbLK.SelectedIndex <> 0 Then


            For i As Integer = 0 To chkListJBGW.Items.Count - 1
                If chkListJBGW.GetItemChecked(i) Then
                    Select Case i
                        Case 0

                            If _1F_Connected = False OrElse _1F_JBGW_PLC.Write("DB1.DBW8", CShort(cmbLK.SelectedIndex)).IsSuccess() = False Then
                                seterrInfo("无法给  1车间法兰板  接驳工位设定料框型号。")
                            End If
                        Case 1

                            If _1G2_Connected = False OrElse _1G2_JBGW_PLC.Write("DB15.DBW10", CShort(cmbLK.SelectedIndex)).IsSuccess() = False Then
                                seterrInfo("无法给  1车间骨架板2  接驳工位设定料框型号。")
                            End If
                        Case 2

                            If _1G3_Connected = False OrElse _1G3_JBGW_PLC.Write("DB1.DBW8", CShort(cmbLK.SelectedIndex)).IsSuccess() = False Then
                                seterrInfo("无法给  1车间骨架板3  接驳工位设定料框型号。")
                            End If
                        Case 3

                            If _2_Connected = False OrElse _2_JBGW_PLC.Write("DB200.DBX78.0", CByte(cmbLK.SelectedIndex)).IsSuccess() = False Then
                                seterrInfo("无法给  2车间法兰板  接驳工位设定料框型号。")
                            End If
                        Case 4

                            If _2_Connected = False OrElse _2_JBGW_PLC.Write("DB200.DBX79.0", CByte(cmbLK.SelectedIndex)).IsSuccess() = False Then
                                seterrInfo("无法给  2车间骨架板  接驳工位设定料框型号。")
                            End If
                    End Select
                End If
            Next
        Else
            seterrInfo("请选择料框大小。")
        End If
    End Sub

    Private Sub btnWaitClose_Click(sender As Object, e As EventArgs) Handles btnWaitClose.Click
        pnlWait.Visible = False
        If IsNothing(thUPWait) = False Then
            Try
                If thUPWait.IsAlive Then
                    thUPWait.Abort()
                End If
            Catch ex As Exception

            End Try
        End If
        If IsNothing(thDNWait) = False Then
            Try
                If thDNWait.IsAlive Then
                    thDNWait.Abort()
                End If
            Catch ex As Exception

            End Try
        End If
    End Sub
    Private Sub seterrInfo(msg As String)
        pnlWait.Visible = True
        pnlWait.BringToFront()
        lblWait.Text = msg
        lblWait.Refresh()
        btnWaitClose.Refresh()
    End Sub

    Private Sub connectJBGW()
        Dim ipAddress1F As String = GetIniFile("JBGW", "1F", "", Me.g_szInitFileName)
        Dim ipAddress1G2 As String = GetIniFile("JBGW", "1G2", "", Me.g_szInitFileName)
        Dim ipAddress2 As String = GetIniFile("JBGW", "2CJ", "", Me.g_szInitFileName)
        Dim ipAddress1G3 As String = GetIniFile("JBGW", "1G", "", Me.g_szInitFileName)
        '==========================连接4个接驳工位PLC==================================
        _1F_JBGW_PLC = New SiemensS7Net(SiemensPLCS.S1200)
        'End Select
        With _1F_JBGW_PLC
            .IpAddress = ipAddress1F
            _1F_JBGW_PLC.Port = 102
            .SetPersistentConnection()
            '_1F_Connected = .ConnectServer().IsSuccess
            'Me.Invoke(_caller1F, 1, _1F_Connected)
        End With

        _1G2_JBGW_PLC = New SiemensS7Net(SiemensPLCS.S1200)
        With _1G2_JBGW_PLC
            .IpAddress = ipAddress1G2
            _1G2_JBGW_PLC.Port = 102
            .SetPersistentConnection()
            '_1G2_Connected = .ConnectServer().IsSuccess
            'Me.Invoke(_caller_1G2, 2, _1G2_Connected)
        End With
        _1G3_JBGW_PLC = New SiemensS7Net(SiemensPLCS.S1200)
        With _1G3_JBGW_PLC
            .IpAddress = ipAddress1G3
            _1G3_JBGW_PLC.Port = 102
            .SetPersistentConnection()

            '_1G3_Connected = .ConnectServer().IsSuccess
            'Me.Invoke(_caller_1G3, 3, _1G3_Connected)
        End With
        _2_JBGW_PLC = New SiemensS7Net(SiemensPLCS.S1200)
        With _2_JBGW_PLC
            .IpAddress = ipAddress2
            _2_JBGW_PLC.Port = 102
            .SetPersistentConnection()
            '_2_Connected = .ConnectServer().IsSuccess
            'Me.Invoke(_caller_2, 4, _2_Connected)
        End With
    End Sub
    Private Sub timer2_Elapsed(sender As Object, e As ElapsedEventArgs) Handles timer2.Elapsed

        If _1F_JBGW_PLC.ReadBool("DB1.DBX0.0").IsSuccess Then
            Me.Invoke(_caller1F, 1, True)
            _1F_Connected = True
        Else
            Me.Invoke(_caller1F, 1, False)
            _1F_Connected = False
        End If

        If _1G2_JBGW_PLC.ReadBool("DB15.DBX0.0").IsSuccess Then
            Me.Invoke(_caller_1G2, 2, True)
            _1G2_Connected = True
        Else
            Me.Invoke(_caller_1G2, 2, False)
            _1G2_Connected = False
        End If

        If _1G3_JBGW_PLC.ReadBool("DB1.DBX0.0").IsSuccess Then
            Me.Invoke(_caller_1G3, 3, True)
            _1G3_Connected = True
        Else
            Me.Invoke(_caller_1G3, 3, False)
            _1G3_Connected = False
        End If

        If _2_JBGW_PLC.ReadBool("DB200.DBX112.0").IsSuccess Then
            Me.Invoke(_caller_2, 4, True)
            _2_Connected = True
        Else
            Me.Invoke(_caller_2, 4, False)
            _2_Connected = False
        End If
        'JBGW_stop_status()
    End Sub

    Private Sub btnAuto_Click(sender As Object, e As EventArgs) Handles btnAuto.Click
        gbAuto.Visible = True
        pnlManual.Visible = False
        btnAuto.BackColor = Color.White
        btnManual.BackColor = Color.Silver
        UcRGV1.手动自动 = 0
    End Sub

    Private Sub btnManual_Click(sender As Object, e As EventArgs) Handles btnManual.Click
        gbAuto.Visible = False
        pnlManual.Visible = True
        btnAuto.BackColor = Color.Silver
        btnManual.BackColor = Color.White
        UcRGV1.手动自动 = 1
    End Sub
End Class
